﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="WindowPlaylist.xaml.cs" company="Jens Horstmann">
//   Copyright (c) Jens Horstmann. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace MiniMusicPlayer.Windows
{
    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;

    using MiniMusicPlayer.Classes;

    /// <summary>
    /// Interaction logic for WindowPlaylist.xaml.
    /// </summary>
    public partial class WindowPlaylist : Window
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="WindowPlaylist"/> class.
        /// </summary>
        /// <param name="window">
        /// The window.
        /// </param>
        public WindowPlaylist(MainWindow window)
        {
            this.MainWindow = window;
            this.InitializeComponent();

            this.DataGridPlaylist.DataContext = this.MainWindow.MusicPlayer.Playlist.Songs;
            this.MainWindow.MusicPlayer.Playlist.ActualSong.ActualSongChanged += this.PlaylistActualSongChanged;
        }

        /// <summary>
        /// Gets or sets the main window.
        /// </summary>
        public MainWindow MainWindow { get; set; }

        /// <summary>
        /// The move.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Move(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        /// <summary>
        /// The playlist actual song changed.
        /// </summary>
        /// <param name="obj">
        /// The obj.
        /// </param>
        private void PlaylistActualSongChanged(Song obj)
        {
            this.DataGridPlaylist.SelectedItem = obj;
            this.LabelActualSong.Content = "Playing: " + obj.ToString();
        }

        /// <summary>
        /// The data grid row key down.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void DataGridRowKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                var row = sender as DataGridRow;
                if (row != null)
                {
                    this.MainWindow.MusicPlayer.Play((Song)row.Item);
                    e.Handled = true;
                }
            }

            if (e.Key == Key.Delete)
            {
                var tempList = new List<Song>();
                foreach (var item in this.DataGridPlaylist.SelectedItems)
                {
                    tempList.Add((Song)item);
                }

                foreach (var song in tempList)
                {
                    this.MainWindow.MusicPlayer.RemoveSong(song);
                }
            }
        }

        /// <summary>
        /// The data grid row double clicked.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void DataGridRowDoubleClicked(object sender, MouseButtonEventArgs e)
        {
            var row = sender as DataGridRow;
            if (row != null)
            {
                this.MainWindow.MusicPlayer.Play((Song)row.Item);
                e.Handled = true;
            }
        }

        private void CloseClicked(object sender, RoutedEventArgs e)
        {
            this.Hide();
        }
    }
}